﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Transcend.FileUploader.Utility;
using CS.UAM;
using System.Collections.Specialized;

namespace Transcend.FileUploader
{
    public partial class UserLoginWindow : Form
    {

        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private string connectionString;

        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }
        private string loginSql;

        public string LoginSql
        {
            get { return loginSql; }
            set { loginSql = value; }
        }
        private LoginUser user;

        public LoginUser User
        {
            get { return user; }
        }

        public UserLoginWindow(string connectionString, string loginSql)
        {
            this.connectionString = connectionString;
            this.loginSql = loginSql;
            InitializeComponent();
        }


        private void Bn_Login_Click(object sender, EventArgs e)
        {
            DbLogin();
        }

        private void Bn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UserLoginWindow_Load(object sender, EventArgs e)
        {

        }

        private void Tb_UserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) DbLogin();
        }

        private void Tb_Password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) DbLogin();
        }



        /// <summary>
        /// 数据库登录
        /// </summary>
        private void DbLogin()
        {
            if (string.IsNullOrEmpty(Tb_Password.Text.Trim()))
            {
                Tb_Password.Focus();
                return;
            }

            if (string.IsNullOrEmpty(Tb_UserName.Text))
            {
                Tb_UserName.Focus();
                return;
            }
            try
            {
                using (var db = new Transcend.DP3.Db.Database(connectionString))
                {
                    user = db.QueryObject<LoginUser>(loginSql, Tb_UserName.Text.Trim(), Tb_Password.Text);
                    if (user != null)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码不正确");
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("登录出错:{0}", ex.Message));
                logger.Error("登录出错", ex);
            }
        }

    }
}
